<?php

namespace App\Console\Commands;

use App\Models\School;
use App\Models\User;
use App\Models\UserRole;
use App\TeSchool\SmartCampus;
use App\TxSchool\Type\Type;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SyncUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步腾讯智慧校园用户';

    private $update = 0;
    private $insert = 0;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @throws \App\TxSchool\Exceptions\UnknownTypeException
     */
    public function handle()
    {
        $schools = School::isActive()->get();
        foreach ($schools as $school) {
            if ($school->objectid){
                $smartCampus = new SmartCampus([
                    'objectid' => $school->objectid,
                    'devCode' => $school->dev_code,
                    'objType' => Type::OBJECT_TYPE_SCHOOL,
                    'key' => $school->key,
                    'keyId'=> $school->key_id
                ], ['developer', 'school']);
                $smartCampus->school->setTypeSchool();
                $defaultRole = UserRole::where('school_id', $school->id)->isDefault()->first();
                $func = function ($department, $page = 1, $size = 99) use ($smartCampus, $school, &$func, $defaultRole) {
                    $res = $smartCampus->school->searchTeacher($department['departid'], $department['level'], $page, $size);
                    dump($res);
                    if ($res['code'] == 0) {
                        foreach ($res['data']['dataList'] as $info) {
                            $user = User::firstOrNew(['userid' => array_get($info, 'userid')]);
                            if (!$user->id) {
                                $user->source_from = 'school';
                                $this->insert += 1;
                                $user->role_id = $defaultRole->id;
                                $user->morning_rule = $school->rule->morning_rule;
                                $user->lunch_rule = $school->rule->lunch_rule;
                                $user->dinner_rule = $school->rule->dinner_rule;
                            } else {
                                $this->update += 1;
                            }
                            $user->school_id = $school->id;
                            $user->userid = array_get($info, 'userid');
                            $user->wxuserid = array_get($info, 'wxuserid');
                            if (array_get($info, 'gender')) {
                                $user->sex = array_get($info, 'gender');
                            }
                            $user->name = array_get($info, 'name');
                            if (array_get($info, 'cellphone')) {
                                $user->phone = array_get($info, 'cellphone');
                            }
                            $user->save();
                        }
                        $currentPage = $res['data']['pageInfo']['page'];
                        $currentSize = $res['data']['pageInfo']['size'];
                        $total = $res['data']['pageInfo']['total'];
                        if ($total > $currentPage * $currentSize) {
                            $page += 1;
                            $func($department, $page, $size);
                        }
                    }
                };
                $loop = function ($data) use ($func, &$loop) {
                    foreach ($data as $department) {
                        $page = 1;
                        $size = 99;
                        $func($department, $page, $size);
                        if (isset($department['child'])) {
                            $loop($department['child']);
                        }
                    }
                };
                $res = $smartCampus->school->getDepartmentInfoList();
                dump($res);
                if ($res['code'] == 0) {
                    $loop($res['data']);
                }
            }
        }

        Log::channel('sync')->info("--------------------------------分隔符---------------------------------");
        Log::channel('sync')->info("同步插入用户:" . $this->insert);
        Log::channel('sync')->info("同步更新用户:" . $this->update);
    }

}
